package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.Recommend;
import com.rockcent.mall.lib.domain.enumclass.RecommandStatus;
import com.rockcent.mall.lib.dto.RecommendDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.RecommendRepository;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangjh on 2016/11/8.
 */
@RestController
public class MallRecommendController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(MallRecommendController.class);


    protected  final  static Long MAX_RECOMMEND_MERCHANT=5l;

    @Autowired
    private BaseUserHelper userHelper;

    @Autowired
    private RecommendRepository recommendRepository;



      /**
       *
      * @api {get} /mall/merchant/recommend/page 商家推荐列表
      * @apiGroup MALL_MERCHANT_RECOMMEND
      * @apiVersion 1.0.0
      * @apiHeader {String} userToken 用户token
      * @apiHeader {String} domain 请求域名
      * @apiParam {String} isRecommend <code>可选参数，值必须为　ALL("ALL")  YES("是") </code>  是否推荐
      * @apiParam {String} merchantName <code>可选参数  </code>  商家名称
      * @apiSuccessExample {json} Success-Response:
      *{
      * "code": "1",
      * "desc": "成功",
      * "data": {
      *     "data": [
      *         {
      *             "merchantId": 48,
      *             "merchantName": "1109349806@qq.com_1",
      *             "taken": false,
      *             "isRecommend": "NO",
       *            "intro":"tttt"
      *         },
      *         {
      *             "merchantId": 83,
      *             "merchantName": "小六子",
      *             "taken": false,
      *             "isRecommend": "YES"
      *         }
      *     ],
      *       "totalCount": 10,
      *       "totalPage": 1,
      *       "pageNo": 0,
      *       "pageSize": 10,
      *       "url": "/mall/merchant/recommend/page",
      *       "prevUrl": "/mall/merchant/recommend/page?pageNo=0&pageSize=10",
      *       "nextUrl": "/mall/merchant/recommend/page?pageNo=0&pageSize=10"
      *     }
      * }
      *
      *
      */
    @RequestMapping(value = Routes.MALL_MERCHANT_RECOMMEND_PAGE, method = RequestMethod.GET)
    public JsonResult listRecommend(RecommendDto recommendDto, HttpServletRequest request) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (StringUtils.isNotBlank(recommendDto.getIsRecommend()) && !EnumUtils.isValidEnum(RecommandStatus.class, recommendDto.getIsRecommend())) {
                jsonResult.remind("isRecommend 值必须为  YES(\"是\"), NO(\"否\")，ALL(\"全部\")", log);
                return;
            }
            PageResult<RecommendDto> result = recommendRepository.getRecommendPageByMallId(userInfoDto.mall.getId(), recommendDto.getMerchantName(), recommendDto.getIsRecommend(), getPage(recommendDto.getPageNo(), recommendDto.getPageSize()));
            result.setUrl(Routes.MALL_MERCHANT_RECOMMEND_PAGE);
            jsonResult.data = result;
        };
        return doing.go(request, log);

    }


     /**
     *
     * @api {put} /mall/merchant/recommend/status/update 商家推荐 推荐状态更新
     * @apiGroup MALL_MERCHANT_RECOMMEND
     * @apiVersion 1.0.0
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain 请求域名
     * @apiParam {String} merchantId <code>必传参数 </code> 商家ID
      * @apiParam {String} isRecommend <code>必传参数 YES/ NO </code> 商家ID
     * @apiSuccessExample {json} Success-Response:
      * {
      *  "code": "1",
      *  "desc": "成功"
      *  }
     *
     */
    @RequestMapping(value = Routes.MALL_MERCHANT_RECOMMEND_STATUS_UPDATE, method = RequestMethod.PUT)
    public JsonResult updateMechantRecommendStatus(@RequestBody RecommendDto recommendDto, HttpServletRequest request) {
        UserInfoDto userInfoDto = userHelper.getSession(request);
        RestDoing doing = jsonResult -> {
            if (recommendDto.getMerchantId() == null) {
                jsonResult.remind("merchantId 不能为空", log);
                return;
            }


            if(Whether.valueOf(recommendDto.getIsRecommend())==Whether.YES){
                Long count= recommendRepository.countByMallIdAndIsDelete(userInfoDto.mall.getId(),Whether.NO);
                if(count==null){
                    count=0l;
                }
                if(count>=MAX_RECOMMEND_MERCHANT){
                    jsonResult.remind("最多只能推荐五个商家", log);
                    return;
                }
            }


            Recommend recommend = recommendRepository.findByMallIdAndMerchantId(userInfoDto.mall.getId(), recommendDto.getMerchantId());

            if (recommend != null) {
                if (recommend.getIsDelete() == Whether.NO) {
                    recommend.setIsDelete(Whether.YES);
                } else {
                    recommend.setIsDelete(Whether.NO);
                }
            } else {
                recommend = new Recommend();
                recommend.setMallId(userInfoDto.mall.getId());
                recommend.setMerchantId(recommendDto.getMerchantId());

            }
            recommendRepository.save(recommend);
        };

        return doing.go(request, log);
    }
}
